package com.example.mybatis.utils;

import com.example.mybatis.exception.MybatisException;
import com.github.houbb.heaven.util.io.StreamUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;


import java.io.InputStream;

public final class XmlUtils {
    public XmlUtils() {
    }

    /**
     * 获取根节点
     *
     * @param contextPath 配置路径
     * @return 元素
     * @since 0.0.1
     */
    public static Element getRoot(String contextPath) {
        try {
            InputStream inputStream = StreamUtil.getInputStream(contextPath);
            SAXReader reader = new SAXReader();
            //try {
            Document document = reader.read(inputStream);
            return document.getRootElement();
        } catch (DocumentException e) {
            throw new MybatisException(e);
        }
        // return  null;
    }
}
